﻿/*
	import com.lbynet.net.LoadData;
	LoadData.getInstance().loadXML("config.xml",getXML);

	function getXML(xml):void {
		
		trace(typeof xml);
		trace(xml);
	} 
*/
package com.lbynet.net {
	
	import flash.events.Event;
	import flash.net.URLLoader
	import flash.net.URLRequest
	import flash.events.IOErrorEvent;
	import flash.system.System;
	
	/**
	 * ...
	 * @author lbynet (Tools -> Custom Arguments...)
	 */
	public class LoadData {
		
		private static var Instance:LoadData = new LoadData();
		private var xmlPath:String;
		private var getXML:Function;
		
		public function LoadData() {
			
		}
		
		public static function getInstance():LoadData {
			return Instance;
		}
		
		/*加载XML文件*/
		public function loadXML(_xmlPath:String, _getXML:Function = null):void {
			
			System.useCodePage = true;
			if (_getXML != null)  getXML = _getXML;
			
			var loader:URLLoader = new URLLoader();
			
			loader.addEventListener(Event.COMPLETE,loadCompleteHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR,loadErrorHandler);
			loader.load(new URLRequest(_xmlPath));
			
		}
		private function loadCompleteHandler(event:Event):void {
			
			var xml:XML = new XML(event.target.data);
			xml.ignoreWhitespace = true;
			
			if (getXML != null) getXML(xml);
			
		}
		private function loadErrorHandler(event:IOErrorEvent):void {
			
			throw new Error("加载失败");
		}
		
		
	}
}